package com.autumn.chat.core;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.autumn.chat.http.DoGPTApi;
import com.autumn.chat.struct.gpt.ChatResponse;
import com.autumn.chat.struct.gpt.Choice;
import com.autumn.chat.struct.gpt.Message;
import com.autumn.chat.struct.gpt.Usage;

import java.util.ArrayList;
import java.util.List;

/**
 * ClassName: GPTService
 * Description:
 * Author: Shen_Yue
 * Date: 2023/3/4 0:48
 * History:
 * <author>          <time>          <version>          <desc>
 * wangqiuyang    2023/3/4 0:48   @Version 1.0        描述
 */
public class GPTService {


    /**
    * Description: 获取gpt的回答
    * @param content
    * @return: java.lang.String
    * @author: Shen_Yue
    * @Date: 1:32 2023/3/4
    */
    public static String getGPTMessage(String content) {

        // 调用接口
        String chatJSON = DoGPTApi.getChatMessage(content);

        // 将结果转化为对象
        ChatResponse chatResponse = convertResult(chatJSON);

        if (chatResponse.getChoices().isEmpty()) {
            // 返回失败 直接丢弃
            return null;
        }

        // 这里可以拿着 ChatResponse 做一些其他业务的逻辑

        // 我们默认那第一个选择， 他有可能给你返回多个回答让你选择
        return chatResponse.getChoices().get(0).getMessage().getContent();
    }
    
    /**
    * Description: http 返回的 结果 转化为 ChatResponse对象
    * @param result
    * @return: com.autumn.chat.struct.gpt.ChatResponse
    * @author: Shen_Yue
    * @Date: 1:34 2023/3/4
    */
    private static ChatResponse convertResult(String result) {
        // body String 转 JSON Obj
        JSONObject chatJSONObject = JSONObject.parseObject(result);

        if (chatJSONObject.getJSONObject("usage")== null) {
            // 由于网络波动链接失效 导致的问题 直接丢弃这个推送
            return new ChatResponse(new ArrayList<>(1));
        }

        // JSON Obj 转 ChatResponse
        ChatResponse res = new ChatResponse();
        res.setId(chatJSONObject.getString("id"));
        res.setObject(chatJSONObject.getString("object"));
        res.setCreated(chatJSONObject.getLongValue("created"));
        res.setModel(chatJSONObject.getString("model"));

        JSONObject usageJSONObject = chatJSONObject.getJSONObject("usage");
        Usage usage = new Usage();
        usage.setPromptTokens(usageJSONObject.getIntValue("prompt_tokens"));
        usage.setTotalTokens(usageJSONObject.getIntValue("total_tokens"));
        usage.setCompletionTokens(usageJSONObject.getIntValue("completion_tokens"));
        res.setUsage(usage);

        JSONArray choiceJSONObjects = chatJSONObject.getJSONArray("choices");
        List<Choice> choices = new ArrayList<>();
        for (Object choiceJSONObject : choiceJSONObjects) {
            Choice choice = new Choice();
            choice.setFinishReason(((JSONObject)choiceJSONObject).getString("finish_reason"));
            choice.setIndex(((JSONObject)choiceJSONObject).getIntValue("index"));

            JSONObject messageJSONObject = ((JSONObject) choiceJSONObject).getJSONObject("message");
            Message message = new Message();
            message.setRole(messageJSONObject.getString("role"));
            message.setContent(messageJSONObject.getString("content"));
            choice.setMessage(message);

            choices.add(choice);
        }
        res.setChoices(choices);

        // 这个是转化好的ChatResponse对象
        return res;
    }

}
